import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ui/api/config.dart';

import 'logic.dart';

class SettingPage extends StatelessWidget {
  SettingPage({super.key});

  final logic = Get.put(SettingLogic());
  final state = Get.find<SettingLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: TextButton(
        onPressed: () => logic.addtopic(),
        style: const ButtonStyle(
          backgroundColor: WidgetStatePropertyAll(Colors.black26),
        ),
        child: const Text(
          "添加主题",
          style: TextStyle(color: Colors.blueAccent),
        ),
      ),
      appBar: AppBar(
        centerTitle: true,
        title: TextButton(
          onPressed: () => logic.saveconfigjson(),
          child: Obx(() {
            return Text(
              "保存配置(点我保存)",
              style: TextStyle(
                  // 有未保存数据 变为红色
                  color: logic.isNoSave.value
                      ? Colors.redAccent
                      : const Color.fromARGB(255, 102, 204, 255),
                  fontSize: 28),
            );
          }),
        ),
      ),
      body: bodyWidget(),
    );
  }

  bodyWidget() => Column(
        children: [
          swichButton(),
          mqttWidget(),
          httpWidget(),
        ],
      );

  swichButton() => Obx(() => SwitchListTile(
      value: logic.switchvalue.value,
      selected: logic.switchvalue.value,
      title:
          logic.switchvalue.value ? const Text("MQTT服务") : const Text("HTTP服务"),
      onChanged: (v) => logic.switchvalue.value = !logic.switchvalue.value));

  httpWidget() => Obx(
        () => Visibility(
          visible: !logic.switchvalue.value,
          child: Column(
            children: [
              const Text("HTTP服务"),
              TextField(
                decoration: const InputDecoration(labelText: "巴法云密钥"),
                maxLines: 1,
                onChanged: (v) => logic.uid.value = v,
                controller: logic.uidController,
              ),
              ListView(
                shrinkWrap: true,
                children: logic.topicView
                    .map(
                      (e) => GestureDetector(
                        onLongPress: () {
                          logic.topicView.remove(e);
                        },
                        child: Container(
                            alignment: Alignment.centerLeft,
                            height: 50,
                            child: Text(
                              "主题：$e",
                              style: TextStyle(fontSize: config.gFontSize),
                            )),
                      ),
                    )
                    .toList(),
              )
            ],
          ),
        ),
      );

  mqttWidget() => Obx(
        () => Visibility(
          visible: logic.switchvalue.value,
          child: Column(
            children: [
              const Text("MQTT服务"),
              TextField(
                onChanged: (v) => logic.mqttServer.value,
                decoration: const InputDecoration(labelText: "MQTT服务器"),
                maxLines: 1,
              ),
              TextField(
                onChanged: (v) => logic.mqttPort.value,
                decoration: const InputDecoration(labelText: "MQTT端口"),
                maxLines: 1,
              ),
              TextField(
                onChanged: (v) => logic.uid.value,
                decoration: const InputDecoration(labelText: "巴法云密钥"),
                maxLines: 1,
                controller: logic.uidController,
              ),
            ],
          ),
        ),
      );
}
